/**
 * Class: OpenLayers.Control.ArcRestPointQuery
 * 
 * 点选控件，实现鼠标点击查询
 * 
 * 继承自: - <OpenLayers.Control>
 * @requires OpenLayers/Control.js
 */

OpenLayers.Control.ArcRestPointQuery = OpenLayers.Class(OpenLayers.Control,{
	/**
	 * Property: type {String} 工具类型
	 */
	type : OpenLayers.Control.TYPE_TOOL,

	/**
	 * Property: styleMap OpenLayers.styleMap
	 */
	styleMap : null,

	/**
	 * Property: tolerance {String} 点选容限
	 */
	tolerance : 1,

	/**
	 * Property: queryFeature {OpenLayers.Geometry} 空间查询构建的多边形
	 */
	queryFeature : 2,

	/**
	 * Property: title {String} 工具按钮提示文字
	 */
	title : "点选",
	
	wmsqueryHandler: null,
	
	poiqueryHandler: null,
	
	proxy: null,
	/**
	 * 
	 */
	querySymbolizers: {
		"Point" : {
			pointRadius : 4,
			graphicName : "square",
			fillColor : "white",
			fillOpacity : 1,
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#333333"
		},
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#666666",
			strokeDashstyle : "dash"
		},
		"Polygon" : {
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#666666",
			fillColor : "white",
			fillOpacity : 0.5
		}
	},

	/**
	 * Method: initialize 初始化
	 */
	initialize : function(options) {
		OpenLayers.Control.prototype.initialize.apply(this,arguments);
		var style = new OpenLayers.Style();
		style.addRules([ new OpenLayers.Rule({
			symbolizer : this.querySymbolizers
		}) ]);
		this.styleMap = new OpenLayers.StyleMap({
			"default" : style
		});
	},

	/**
	 * Method: activate 控件被触发时设置鼠标手势
	 */
	activate : function() {
		OpenLayers.Control.prototype.activate.apply(this,arguments);
		// this.map.layerContainerDiv.style.cursor = "url("
				// + AT21.WebGIS.getAT21RootLocation()
				// + "img/PointQuery.cur),default";
	},

	/**
	 * Method: draw
	 */
	draw : function() {
		this.handler = new OpenLayers.Handler.Click(this, {
			click : this.executeQuery
		}, {
			keyMask : this.keyMask,
			layerOptions : {
				styleMap : this.styleMap
			}
		});
	},

	/**
	 * Method: executeQuery 响应点击事件
	 * 
	 * Parameters: evt - {{<OpenLayers.Event>}
	 */
	executeQuery : function(evt) {
		var geometry = this.evtToCoord(evt);
		this.event = evt;
		this.queryFeature = geometry;
		this.spatialQuery(geometry);
	},
	
	/**
	 * evt转为空间对象geometry 
	 */
	evtToCoord : function(evt) {
		var mapResolution = this.map.getResolution();
		var lonlat = this.map.getLonLatFromViewPortPx(evt.xy);
		// 构造点查询小矩形
		var top = lonlat.lat + mapResolution * this.tolerance / 2;
		var right = lonlat.lon + mapResolution * this.tolerance / 2;
		var bottom = lonlat.lat - mapResolution * this.tolerance / 2;
		var left = lonlat.lon - mapResolution * this.tolerance / 2;

		var geometrystr = left+","+ bottom + "," + right + "," + top;
		// 将小矩形坐标构造成geometry对象
		return geometrystr;
	},

	/**
	 * Method: spatialQuery 构造查询条件并提交
	 * 
	 * Parameters: evt
	 */
	spatialQuery : function(geometry) {
		// 查询之前事件回调
		var flag = this.map.events.triggerEvent("preQuery", {
			"queryControl" : this
		});
		// 如果有回调函数并且回调函数返回了false则不运行默认的查询
		if (flag != undefined && !flag){
			return;
		}
		
		var scope = this;
		// 待实现 --构建queryobject 提交查询queryHandler 默认为全图层查询
		var wmsqos = this.getWMSQueryObjects(geometry);
		var poiqos = this.getPOIQueryObjects(geometry);
		if (wmsqos != null && wmsqos.length > 0) {
			this.wmsqueryHandler = new OpenLayers.ArcRestWMSQueryHandler(wmsqos, function(results, evt){
				scope.queryend(results, evt);
			},{
				proxy: this.proxy
			});
			this.wmsqueryHandler.requestData(this.event);
		}
		if (poiqos != null && poiqos.length > 0) {
			if(OpenLayers.PoiQueryHandler){
				this.poiqueryHandler = new OpenLayers.PoiQueryHandler(poiqos, function(results, evt){
					scope.queryend(results);
				});
				this.poiqueryHandler.requestData(this.event);
			}
		}

		// 查询之后事件回调
		this.map.events.triggerEvent("postQuery", {
			"queryControl" : this
		});
	},
	
	/**
	 * 
	 */
	queryend: function(){
		
	},
	
	/**
	 * Method: getPOIQueryObjects 构建POI查询参数对象数组
	 * 
	 */
	getPOIQueryObjects : function(geometry) {
		if (geometry != null) {
			var poiqueryObjectArray = [];
			// 获取图层对象
			var layers = this.map
					.getLayersByClass("OpenLayers.Layer.PoiLayer");
			if (layers != null && layers.length > 0) {
				for ( var i = 0; i < layers.length; i++) {
					var poiqo;
					// 如果可查属性设置了，则获取设置的其他查询参数
					if (layers[i].spetialQueryable && layers[i].visibility) {
							poiqo = {
								name: layers[i].name,
								coords : geometry,
								layer : layers[i],
								returnGeometry : 'true',
								spatialRelationShip : 'intersaction'
							};
					} else {
						// 如果没有设置可查属性则查询可见图层
					}
					poiqueryObjectArray.push(poiqo);
				}
			}
			return poiqueryObjectArray;
		}
	},

	/**
	 * Method: getWMSQueryObjects 构建WMS查询参数对象数组
	 * 
	 */
	getWMSQueryObjects : function(geometry) {
		if (geometry != null) {
			var wmsqueryObjectArray = [];
			// 获取图层对象
			var wmslayers = this.map
					.getLayersByClass("OpenLayers.Layer.WMS");
			if (wmslayers) {
				for ( var i = 0; i < wmslayers.length; i++) {
					var wmsqo;
					var layer = wmslayers[i];
					// 如果可查属性设置了，则获取设置的其他查询参数
					if (layer.spetialQueryable && layer.visibility && layer.restIDs) {
						wmsqo = {
							name: layer.name,
							url: layer.url,
							coords : geometry,
							titles: layer.titles,
							subLayers : layer.restIDs,			//此处必须是rest中该图层对应的图层id号
							returnGeometry : layer.returnGeometry,
							spatialRelationShip : 'Intersects'
						};
						wmsqueryObjectArray.push(wmsqo);
					}
				}

			}
			return wmsqueryObjectArray;
		}
	},


	CLASS_NAME : "OpenLayers.Control.ArcRestPointQuery"
});